package com.cxm.support.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cxm.support.entity.User;
import com.cxm.support.service.UserService;
import com.cxm.support.vo.DataVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxm
 * @since 2022-05-12
 */
@Controller
@RequestMapping("/support")
public class UserController {
    @Autowired
    private UserService userService;


    @GetMapping("/userlistdata")
    @ResponseBody
    @RequiresPermissions("用户管理")
    public DataVO Userlist(Integer page, Integer limit) {

        return userService.findData(page, limit);
    }


    //    删除
    @PostMapping("/userlistdelete")
    @ResponseBody
//    @RequiresPermissions("用户删除")
    public String lotteryDelete(Integer id) {

        userService.removeById(id);
        return null;

    }
    //    保存
    @RequestMapping("/userlistadd")
    @ResponseBody
    @RequiresPermissions("用户管理")
    public String userlist(@RequestParam("username") String username, @RequestParam("password") String password,
                           @RequestParam("checkedData") String checkedData
                           )  {

        //取出权限值
        String s = new String();
        List listObjectEigh = JSONObject.parseArray(checkedData, Map.class);
        for (Object mapList : listObjectEigh) {
            for (Object entry : ((Map) mapList).entrySet()) {
                //找到子类
                if (((Map.Entry) entry).getKey().equals("children")) {
                    Object value = ((Map.Entry) entry).getValue();
                    List list = JSONArray.parseArray(JSON.toJSONString(value), Map.class);
                    for (Object mappp : list) {
                        for (Object entry2 : ((Map) mappp).entrySet()) {
                            if (((Map.Entry) entry2).getKey().equals("title")) {

                                s = s + "," + ((Map.Entry) entry2).getValue();

                            }
                        }
                    }

                }
            }
//    }
            //取出所有权限值后

            User user = new User();
            user.setPerms(s);
            user.setUsername(username);
            user.setPassword(password);

            userService.save(user);
            return null;

        }

        return null;



    }

    @PostMapping("/userlistupdate")
    @ResponseBody
//    @RequiresPermissions("用户编辑")
    public String lotteryUpdate(@RequestParam("username") String username,@RequestParam("password") String  password,@RequestParam("id") String  id) {
        User User = new User();
        User.setUsername(username);
        User.setPassword(password);
        User.setId(Integer.valueOf(id).intValue());
        userService.updateById(User);
        return null;
    }




















    }



























